package com.wwk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.wwk.constant.MessageConstant;
import com.wwk.entity.PageResult;
import com.wwk.entity.QueryPageBean;
import com.wwk.entity.Result;
import com.wwk.pojo.CheckGroup;
import com.wwk.pojo.CheckItem;
import com.wwk.service.CheckGroupService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @Classname CheckGroupController
 * @Description TODO
 * @Date 2020/10/31 19:16
 * @Created by com.wwk
 * @Author com.wwk
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService groupService;

    @RequestMapping("/findAllForMeal")
    public Result findAllForMeal(){
        List<CheckGroup> checkGroupList = groupService.findAllForMeal();
        if(checkGroupList != null && checkGroupList.size() > 0){
            Result result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            result.setData(checkGroupList);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    @RequestMapping("/findAll")
    public PageResult findAll(@RequestBody QueryPageBean pageBean){
        return groupService.findPage(pageBean);
    }

    @RequestMapping("/findAllItem")
    public Result findAllItem(){
        //创建CheckItem的列表
        List<CheckItem> items=groupService.findAllItem();
        //判断item是否存在
        if(items!=null && items.size()>0){
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,items);
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        System.out.println(checkGroup.toString());
        System.out.println(Arrays.toString(checkitemIds));
        //checkGroup用于添加到数据库中，itemIds用于添加到对应的检查组_检查项表中
        try{
            groupService.add(checkGroup,checkitemIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try{
            //修改时修改对应属性，且更新检查项列表
            groupService.edit(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findById/{groupId}")
    public Result findById(@PathVariable Integer groupId){
        try{
            CheckGroup checkGroup=groupService.findById(groupId);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/deleteById/{groupId}")
    public Result deleteById(@PathVariable Integer groupId){
        try{
            groupService.deleteById(groupId);
            return  new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    //找到检查组的选中的所有检查项
    @RequestMapping("/findAllGroupItemIds/{groupId}")
    public Result findAllGroupItemIds(@PathVariable Integer groupId){
        try{
            List<Integer> itemIds=groupService.findAllGroupItemIds(groupId);
            return new Result(true,"查询检查组选中的检查项成功！",itemIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询检查组选中的检查项失败！");
        }
    }
}
